package main

import (
	"encoding/json"
	"fmt"
)

/**
序列化: json.Marshal(data interface{})
反序列化: json.UnMarshal(data []byte,  v  interface{})
 */

type Person struct {
	Name string `json:"name"`
	Age  int `chinaL:"dabai"`
}

func main() {
	run()

}

func run(){
	person := Person { "tom", 12 }

	//json 序列化
	b, err := json.Marshal(person)
	if err != nil {
		fmt.Println("error:", err.Error())
	} else {
		data := string(b)
		fmt.Println("value:", data)
	}

	per := new(Person)
	errs := json.Unmarshal(b,&per) //反序列化
	if errs == nil {
		fmt.Println("value:", per)
	}


	//map 序列化
	mapa := map[int]string{1: "world"}
	b2, _ := json.Marshal(mapa)
	fmt.Println("mapa:", string(b2))

	per2 := map[int]string{}
	json.Unmarshal(b2,&per2) //反序列化
	fmt.Println("mapa:", per2)
}